package com.xsp.controller;

import com.xsp.entity.User;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @author xsp
 * @date 2024/2/5
 * @desc 处理和响应用户请求的控制器
 */
public class UserController implements Controller {
    @Override
    public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
        // 查询用户列表
        // 假设Model查询到了
        List<User> userList = new ArrayList<>();
        userList.add(new User(1,"tom1","111"));
        userList.add(new User(2,"tom2","111"));
        userList.add(new User(3,"tom3","111"));
        userList.add(new User(4,"tom4","111"));
        // 以前玩法：将数据保存到request中，然后转发到userList.jsp
        ModelAndView mv = new ModelAndView();
        // request.setAttribute("userList",userList)
        mv.addObject("userList",userList);
        // 转发，request.getRequestDispatcher("WEB-INF/jsp/userlist.jsp").forward(res,resp)
//        mv.setViewName("/WEB-INF/jsp/userlist.jsp");
        mv.setViewName("userlist"); // 由视图解析器自动拼凑成上述地址格式
        return mv;
    }
}
